<template>
  <div>
    <CheckboxGroup v-model="checkList" @change="handleChange">
      <Checkbox v-for="item in $props.options" :key="item.label" v-bind="item">
        {{ item.value }}
      </Checkbox>
    </CheckboxGroup>
  </div>
</template>

<script>
import { defineComponent, ref, watch } from 'vue'
import { CheckboxGroup, Checkbox } from 'element-ui'

export default defineComponent({
  name: 'BasicCheckboxGroup',
  components: { CheckboxGroup, Checkbox },
  props: {
    value: {
      type: [Array, String],
      default: () => []
    },
    options: {
      type: Array,
      default: () => []
    }
  },
  emits: ['options-change'],
  setup(props, { emit }) {
    const checkList = ref([])

    watch(
      () => props.value,
      value => {
        checkList.value = Array.isArray(value) ? value : [value]
      },
      {
        immediate: true
      }
    )

    function handleChange(e) {
      emit('options-change', e)
    }

    return {
      checkList,
      handleChange
    }
  }
})
</script>
